/*
  Copyright (C) 2013 Napspirit / Thierry Rochelet

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


package com.napspirit.tmx.ui;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import com.napspirit.tmx.data.TMXTU;

public class TMXTUPanel extends JPanel 
    implements DocumentListener , ActionListener {

    private Frame parent;
    private Hashtable<Locale, JTextArea> locales;

    private JTextField tuidField;
    private JComboBox category;

    private JTextArea noteField;
    
    private boolean modified;

    public TMXTUPanel(Frame parent) {
        this.parent = parent;
        this.buidPanel();
    }

    public boolean isModified () { return this.modified; }


    /**
     * Rebuilds and refreshes the view panel, this panel displays the key we are
     * editing and its values in each locale we are working with.
     *
     * @return view panel to be added to the main window
     */
    public void buidPanel() {
        this.removeAll();
        this.setLayout(new BorderLayout());                
        this.locales = new Hashtable<Locale,JTextArea>();
        this.modified = false;


        // Get current stuff !
        TMXTU tu = this.parent.getTMX().getCurrent();
        if (tu == null) {
            this.parent.current_tu_empty();
            return;
        }

        this.parent.current_tu_displayed();

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,2));                

        // TUID
        panel.add(new JLabel("TUID: "));
        this.tuidField = new JTextField();
        this.tuidField.setText(tu.getID());
        this.tuidField.getDocument().addDocumentListener(this);
        panel.add(this.tuidField);

        // Category
        panel.add(new JLabel("Category: "));
        this.category = new JComboBox();
        this.category.addItem(""); // no filter
        Enumeration<String> categories = this.parent.getTMX().getCategories();
        while (categories.hasMoreElements()) {
            String cat = categories.nextElement();
            this.category.addItem(cat);
        }
        this.category.setSelectedItem(tu.getCategory());
        this.category.addActionListener(this);
        panel.add(this.category);
        

        this.add(panel,BorderLayout.NORTH);

        // tu::@category filter
        

        // TUVS
        JPanel tuvs_panel = new JPanel();
        tuvs_panel.setLayout(new BoxLayout(tuvs_panel, BoxLayout.PAGE_AXIS));

        Enumeration<Locale> _locales = tu.getLocales();
        while (_locales.hasMoreElements()) {
            Locale locale = _locales.nextElement();

            JPanel locPanel = new JPanel();
            locPanel.setLayout(new BorderLayout());

            JTextArea text = new JTextArea();
            text.setLineWrap(true);
            text.setWrapStyleWord(true);
            text.setText(tu.getLocale(locale));
            text.getDocument().addDocumentListener(this);

            this.locales.put(locale,text);

            String lang = locale.getDisplayLanguage()+" ("+locale.getISO3Language()+")";
            TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),lang);
            locPanel.add(text,BorderLayout.CENTER);
            locPanel.setBorder(title);

            tuvs_panel.add(locPanel);
        }

        // Translation unit
        JScrollPane sp = new JScrollPane(tuvs_panel);
        sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        // Add border to view panel
        TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"Translation unit");
        tuvs_panel.setBorder(title);
        this.add(tuvs_panel, BorderLayout.CENTER);

        JPanel footerPanel = new JPanel();
        footerPanel.setLayout(new BoxLayout(footerPanel, BoxLayout.PAGE_AXIS));

        // note
        this.noteField = new JTextArea();
        this.noteField.setText(tu.getNote());
        this.noteField.getDocument().addDocumentListener(this);
        title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"Note");
        this.noteField.setBorder(title);
        footerPanel.add(this.noteField);

        // reference
        JTextArea ref = new JTextArea();
        StringBuffer buf = new StringBuffer();
        Enumeration<String> _ref = tu.getReferences();
        while (_ref.hasMoreElements()) {
            buf.append(_ref+"\n");
        }        
        ref.setEditable(false);
        ref.setText(buf.toString());
        if (tu.hasReferences()) {
            title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"References");
        }
        else {
            title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"Not USED");
        }


        ref.setBorder(title);
        footerPanel.add(ref);

        this.add(footerPanel,BorderLayout.SOUTH);
    }


    public void refresh() {
        this.buidPanel();
        this.parent.pack();
        this.repaint();

    }

    public String getTUID() { return this.tuidField.getText(); }

    public void save () {
        TMXTU tu = this.parent.getTMX().getCurrent();
        if (tu == null) {
            return;
        }
        if (!this.modified) {
            return;
        }

        tu.setID(this.tuidField.getText());
        tu.setNote(this.noteField.getText());
        tu.setCategory((String)this.category.getSelectedItem());
        Enumeration<Locale> _locales = this.locales.keys();
        while (_locales.hasMoreElements()) {
            Locale locale = _locales.nextElement();
            JTextArea text = this.locales.get(locale);
            tu.setLocale(locale,text.getText());
        }

        this.modified = false;
        this.parent.current_tu_displayed();
    }

    public void changedUpdate(DocumentEvent e) {
        this.modified = true;
        this.parent.current_tu_modified();
    }

    public void insertUpdate(DocumentEvent e) {
        this.modified = true;
        this.parent.current_tu_modified();
    }

    public void removeUpdate(DocumentEvent e) {
        this.modified = true;
        this.parent.current_tu_modified();
    }

    public void actionPerformed(ActionEvent event) {
        this.modified = true;
        this.parent.current_tu_modified();
    }

}
